In [1]:
# Data analysis from player and 
In [2]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express  as px

folder_path = '../assets/Assignment_Data_2023-2024/Assignment_Data_2023-2024/'

# Carica i dati dal file CSV
data = pd.read_csv(folder_path+'appearances.csv')
appearances_df = pd.read_csv(folder_path+'appearances.csv')
In [2]:
 

Data analysis for the top 20 scorers¶

In [3]:
# Raggruppa per nome del giocatore e somma dei gol segnati
goals_per_player = data.groupby('player_name')['goals'].sum()

# Trova i giocatori che hanno segnato di più e ordina per numero di gol decrescente
top_scorers = goals_per_player.nlargest(20).sort_values(ascending=False)

# Crea una tabella dei migliori marcatori
top_scorers_table = pd.DataFrame({'Player': top_scorers.index, 'Goals scored': top_scorers.values})

# Crea il grafico con Plotly Express
fig = px.bar(top_scorers_table, x='Goals scored', y='Player', title='Top 20 players with highest goals scored', 
             labels={'Goals scored':'Goals scored', 'Player':'Player'}, orientation='h')  # orientation='h' per avere le barre orizzontali

# Mostra il grafico
fig.show()
In [4]:
import pandas as pd
import plotly.express as px

# Carica i dati dal file CSV
data = pd.read_csv(folder_path+'appearances.csv')  # Assicurati che il percorso del file sia corretto

# Raggruppa per nome del giocatore e somma dei cartellini rossi
red_card_per_player = data.groupby('player_name')['red_cards'].sum()

# Trova i giocatori che hanno ricevuto più cartellini rossi
top_scorers = red_card_per_player.nlargest(30)  # Puoi cambiare il numero di giocatori mostrati modificando il parametro

# Crea una tabella dei migliori giocatori
top_scorers_table = pd.DataFrame({'Player': top_scorers.index, 'Red cards': top_scorers.values})

# Crea il grafico con Plotly Express
fig = px.scatter(top_scorers_table, x='Red cards', y='Player', title='Top 30 players with highest red cards', labels={'Red cards':'Red cards', 'Player':'Player'})

# Mostra il grafico
fig.show()
In [5]:
# Carica i dati dal file CSV
data = pd.read_csv(folder_path+'appearances.csv')

# Raggruppa per nome del giocatore e somma dei cartellini gialli
yellow_card_per_player = data.groupby('player_name')['yellow_cards'].sum()

# Trova i giocatori che hanno ricevuto più cartellini gialli
top_scorers = yellow_card_per_player.nlargest(30)  # Puoi cambiare il numero di giocatori mostrati modificando il parametro

# Crea una tabella dei migliori giocatori
top_scorers_table = pd.DataFrame({'Player': top_scorers.index, 'Yellow cards': top_scorers.values})

# Crea il grafico con Plotly Express
fig = px.scatter(top_scorers_table, x='Yellow cards', y='Player', title='Top 30 players with highest yellow cards', labels={'Yellow cards':'Yellow cards', 'Player':'Player'})

# Mostra il grafico
fig.show()

Data analysis how the red card and yellow card are influenced on Robert Lewandowski performance¶

In [6]:
goals_per_player = appearances_df.groupby('player_name')['goals'].sum()
top_scorer = goals_per_player.idxmax()
red_card_per_player = appearances_df.groupby('player_name')['red_cards'].sum()
yellow_card_per_player = appearances_df.groupby('player_name')['yellow_cards'].sum()
print(f"Top scorer: {top_scorer}")
print(f"Red cards for top scorer: {red_card_per_player[top_scorer]}")
print(f"Yellow cards for top scorer: {yellow_card_per_player[top_scorer]}")
Top scorer: Robert Lewandowski
Red cards for top scorer: 1
Yellow cards for top scorer: 53

Data analysis for the top scorer performance overview analysis goals , red cards and yellow cards¶

In [7]:
import pandas as pd
import plotly.graph_objects as go

# Carica i dati (sostituisci con il tuo percorso e nome file)
# Esegui il caricamento dei dati come necessario
# appearances_df = pd.read_csv('appearances.csv')

# Per il contesto di esempio, creiamo dati simulati


# Raggruppa per nome del giocatore e somma dei gol segnati
goals_per_player = appearances_df.groupby('player_name')['goals'].sum()

# Trova il miglior marcatore
top_scorer = goals_per_player.idxmax()

# Raggruppa per nome del giocatore e somma dei cartellini rossi
red_card_per_player = appearances_df.groupby('player_name')['red_cards'].sum()

# Raggruppa per nome del giocatore e somma dei cartellini gialli
yellow_card_per_player = appearances_df.groupby('player_name')['yellow_cards'].sum()

# Crea un DataFrame per i migliori marcatori, cartellini rossi e cartellini gialli
top_players_df = pd.DataFrame({
    'Player': goals_per_player.index,
    'Goals': goals_per_player.values,
    'Red Cards': red_card_per_player.values,
    'Yellow Cards': yellow_card_per_player.values
})

# Ordina per numero decrescente di gol segnati per avere una migliore visualizzazione
top_players_df = top_players_df.sort_values(by='Goals', ascending=False)

# Prepara i dati per il grafico
players = top_players_df['Player']
goals = top_players_df['Goals']
red_cards = top_players_df['Red Cards']
yellow_cards = top_players_df['Yellow Cards']

# Creazione del grafico interattivo con Plotly
fig = go.Figure()

# Aggiungi le barre per i gol segnati, cartellini rossi e gialli
fig.add_trace(go.Bar(
    y=players,
    x=goals,
    name='Goals',
    orientation='h',
    marker=dict(color='blue' , line=dict(color='blue', width=2.5 ) ),
    hovertemplate='Player: %{y}<br>Goals: %{x}',
))

# Aggiungi i marker a bolle per i cartellini rossi
fig.add_trace(go.Scatter(
    y=players,
    x=red_cards,
    mode='markers',
    name='Red Cards',
    marker=dict(color='red', symbol='square', size=12),
    hovertemplate='Player: %{y}<br>Red Cards: %{x}',
))

# Aggiungi i marker a bolle per i cartellini gialli
fig.add_trace(go.Scatter(
    y=players,
    x=yellow_cards,
    mode='markers',
    name='Yellow Cards',
    marker=dict(color='yellow', symbol='circle', size=10),
    hovertemplate='Player: %{y}<br>Yellow Cards: %{x}',
))

# Aggiungi il titolo e le etichette degli assi
fig.update_layout(
    title='Performance Overview of Players',
    xaxis_title='Count',
    yaxis_title='Player',
    legend_title='Metrics',
    barmode='stack',
    hovermode='closest',
    height=800,
    template='plotly_dark'  # Scegli il tema che preferisci
)

# Mostra il grafico
fig.show()